<template>
	<view class="newsPage">
		<view class="new_item" v-for="(item,index) in newsList" :key="index" @tap="open(item)">
			<image :src="item.img_url"></image>
			<view class="content">
				<view class="tit">{{item.title}}</view>
				<view class="info">
					<text>发表时间 : {{item.add_time | formDate}}</text>
					<text>浏览 : {{item.click}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Time from "@/common/js/time.js";
	export default {
		onLoad() {
			// 获取资讯数据列表
			this.getNewsList();
		},
		data() {
			return {
				newsList: [],
			};
		},
		methods: {
			async getNewsList() {
				let res = await uniCloud.callFunction({
					name: 'news',
					data: {
						methods: "get"
					}
				});
				this.newsList = res.result.data;
			},
			open(item) {
				uni.navigateTo({
					url: "../../newsDetail/newsDetail",
					success: res => {
						item.click++;
						res.eventChannel.emit('newsPage', {
							item,
						});
						this.clickNum(item._id, item.click);
					}
				});
			},
			async clickNum(id, num) {
				let res = await uniCloud.callFunction({
					name: 'news',
					data: {
						methods: "clickNum",
						id,
						num
					}
				});
			},
		},
		filters: {
			formDate(value) {
				return new Time().formTime(Number(value));
			}
		},
	}
</script>

<style lang="scss">
	.new_item {
		display: flex;
		padding: 10rpx 20rpx;
		border-bottom: 1px solid #b50e03;

		>image {
			width: 300rpx;
			height: 150rpx;
			border-radius: 10rpx;
		}

		.content {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-left: 15rpx;

			.tit {
				font-size: 30rpx;
			}

			.info {
				@include flexBetweenCenter;
				font-size: 28rpx;
				padding-right: 20rpx;
			}
		}
	}
</style>
